import java.awt.*;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class AWTApp extends javax.swing.JFrame {

	private static final long serialVersionUID = 1L;
	// The window size is fixed
	static final int WIDTH = 950;
	static final int HEIGHT = 250;
	private MenuBar menuBar;
	boolean newWord = false;
	boolean forwardStep = true;
	boolean shown = false;
	TextComponent word = new TextField("word", 55);
	TextComponent meaning = new TextField("meaning", 55);
	TextComponent counter = new TextField("0", 55);
	final WordTest wt;
	Word nxtPal = new Word("", "");

	public AWTApp(final WordTest wt) {

		super("WordTest application");

		// Menus are created
		InitMenus();

		this.wt = wt;

		this.nxtPal = new Word("", "");

		setFont(new Font("TimesRoman", Font.PLAIN, 12));
		setBackground(Color.white);
		setForeground(Color.black);
		setLayout(new BorderLayout());

		// Incorporamos dos botones en la parte inferior de la
		// ventana
		Panel p = new Panel();
		Panel p2 = new Panel();

		p.add(new Button("Step forward"));
		p.add(new Button("Step backwards"));
		p.add(new Button("Next set"));
		p.add(new Button("Mark"));
		p.add(new Button("Unmark"));
		add("South", p);
		add("North", p2);
		// add( "Center",p3 );
		this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);

		// Ajustamos el tamanio de la ventana a nuestras necesidades
		// y la hacemos visible en pantalla
		pack();
		resize(WIDTH, HEIGHT);
		setLocation(500, 300);
		show();

		new FileDrop(System.out, p, /* dragBorder, */new FileDrop.Listener() {
			public void filesDropped(java.io.File[] files) {
				for (int i = 0; i < files.length; i++) {
					try {
						wt.moreFiles(files[i].getCanonicalPath());
					} // end try
					catch (java.io.IOException e) {
					}
				} // end for: through each dropped file
			} // end filesDropped
		}); // end FileDrop.Listener

	}

	/**
	 * The menu bar is created
	 */
	private void InitMenus() {
		menuBar = new MenuBar();

		// Opcion File del menu y las subopciones
		Menu m = new Menu("File");
		m.add(new MenuItem("Open"));
		m.addSeparator();
		m.add(new MenuItem("Close"));
		menuBar.add(m);

		// Opcion Help del menu y subopciones
		m = new Menu("Help");
		m.add(new MenuItem("Help!"));
		m.addSeparator();
		m.add(new MenuItem("Acerca de..."));
		menuBar.add(m);

		setMenuBar(menuBar);

	}

	public void paint(Graphics g) {
		// Redraw
		g.setColor(Color.white);
		g.fillRect(0, 0, 6000, 3000);
		if (this.shown) {
			g.setFont(new Font("TimesRoman", Font.BOLD, 24));
			g.setColor(Color.blue);
			g.drawString(nxtPal.getWord(), 60, 100);
			g.drawString(nxtPal.getMeaning(), 60, 140);
		} else if (this.forwardStep) {
			g.setFont(new Font("TimesRoman", Font.BOLD, 24));
			g.setColor(Color.blue);
			g.drawString(nxtPal.getWord(), 60, 100);
		} else {
			g.setFont(new Font("TimesRoman", Font.BOLD, 24));
			g.setColor(Color.blue);
			g.drawString(nxtPal.getMeaning(), 60, 140);
		}
		g.setColor(Color.white);
		g.fillRect(60, 160, 6000, 40);
		g.setFont(new Font("TimesRoman", Font.BOLD, 12));
		g.setColor(Color.BLACK);
		g.drawString("Rem: " + this.wt.remainingWords(), 60, 180);
	}

	public boolean handleEvent(Event evt) {

		Graphics g = getGraphics();

		g.setColor(Color.white);
		g.fillRect(60, 160, 6000, 40);
		g.setFont(new Font("TimesRoman", Font.BOLD, 12));
		g.setColor(Color.BLACK);
		g.drawString("Rem: " + this.wt.remainingWords(), 60, 180);

		if ("Step forward".equals(evt.arg)) {
			this.forwardStep = true;
			if (meaning.getText().equals("")) {
				this.shown = true;
				this.meaning.setText(nxtPal.getMeaning());
				g.setColor(Color.white);
				g.fillRect(60, 115, 6000, 40);
				g.setFont(new Font("TimesRoman", Font.BOLD, 24));
				g.setColor(Color.blue);
				g.drawString(nxtPal.getMeaning(), 60, 140);

			} else {
				this.shown = false;
				nxtPal = wt.nextWord();
				this.word.setText(nxtPal.getWord());

				g.setColor(Color.white);
				g.fillRect(60, 70, 6000, 40);

				g.setFont(new Font("TimesRoman", Font.BOLD, 24));
				g.setColor(Color.blue);
				g.drawString(nxtPal.getWord(), 60, 100);

				this.meaning.setText("");

				g.setColor(Color.white);
				g.fillRect(60, 115, 6000, 40);
			}
			return true;
		}
		if ("Step backwards".equals(evt.arg)) {
			this.forwardStep = false;
			if (word.getText().equals("")) {
				this.shown = true;
				g.setColor(Color.white);
				g.fillRect(60, 70, 6000, 40);

				g.setFont(new Font("TimesRoman", Font.BOLD, 24));
				g.setColor(Color.blue);
				g.drawString(nxtPal.getWord(), 60, 100);
				this.word.setText(nxtPal.getWord());
			} else {
				this.shown = false;
				nxtPal = wt.nextWord();
				this.meaning.setText(nxtPal.getMeaning());

				g.setColor(Color.white);
				g.fillRect(60, 115, 6000, 40);

				g.setFont(new Font("TimesRoman", Font.BOLD, 24));
				g.setColor(Color.blue);
				g.drawString(nxtPal.getMeaning(), 60, 140);

				this.word.setText("");

				g.setColor(Color.white);
				g.fillRect(60, 70, 6000, 40);

			}

			return true;
		}
		if ("Next set".equals(evt.arg)) {
			wt.nextSet();
			return true;
		}
		if ("Mark".equals(evt.arg)) {
			wt.mark();
			return true;
		}
		if ("Unmark".equals(evt.arg)) {
			wt.unmark();
			return true;
		}
		switch (evt.id) {
		// The window is destroyed.
		case Event.WINDOW_DESTROY:
			System.exit(0);
			return true;

		case Event.ACTION_EVENT:
			if (evt.target instanceof MenuItem) {
				if ("Open".equals(evt.arg)) {
					nuFile();
				}
				if ("Close".equals(evt.arg))
					System.exit(0);
				if ("Help!".equals(evt.arg))
					System.out.println("No help file available yet.");
				if ("Acerca de...".equals(evt.arg)) {
					AboutDialog ad = new AboutDialog(this);
					ad.move(400, 560);
					ad.show();
				}
			}
		default:
			return false;
		}
	}

	public void nuFile() {
		JFileChooser fc = new JFileChooser();
		int returnVal = fc.showOpenDialog(this);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			// The file is opened and its information is extracted
			File file = fc.getSelectedFile();
			try {
				this.wt.moreFiles(file);
			} catch (Exception ex) {
				System.out
						.println("Fatal error: \nPlease, select a well formatted file.");
			}
		} else {
			System.out.println("Open command cancelled by user.");
		}
	}
}

/**
 * About dialog class.
 * @author fjrd84
 *
 */
class AboutDialog extends Dialog {

	private static final long serialVersionUID = 1L;
	static int WIDTH = 300;
	static int HEIGHT = 150;

	// About dialog
	public AboutDialog(Frame parent) {
		super(parent, "Acerca de...", true);
		this.setResizable(false);
		setBackground(Color.gray);
		setLayout(new BorderLayout());

		Panel p = new Panel();
		p.add(new Button("Aceptar"));
		add("South", p);
		resize(WIDTH, HEIGHT);
	}

	// Info about the application.
	public void paint(Graphics g) {
		g.setColor(Color.white);
		g.drawString("Languages learning tool", WIDTH / 4,
				HEIGHT / 3);
		g.drawString("Versi�n 0.1.0", WIDTH / 3 + 15, HEIGHT / 3 + 20);
	}

	public boolean handleEvent(Event evt) {
		switch (evt.id) {
		// "Aceptar" hides the dialog.
		case Event.ACTION_EVENT:

			if ("OK".equals(evt.arg)) {
				System.out.println("You pressed OK\n");
				hide();
				return true;
			}
		default:
			return false;
		}
	}
}
